package pixelwaks.gui.impl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SpringLayout;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import common.i18n.ChangeLanguage;
import common.i18n.IChangeLanguage;
import common.util.FileHandler;

import pixelwaks.ctrl.ControlFactory;



public class EditModePanel extends JPanel implements IChangeLanguage{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JLabel title = new JLabel();

	private JPanel mListPanel = new JPanel();
	private List<String> modes;
	private JList mList;
	private JScrollPane mSPane;

	private TitledBorder modeMBorder = BorderFactory.createTitledBorder("");
	private Border modeSBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);

	private JButton modeFButton = new JButton();

	public EditModePanel() {
		ChangeLanguage.addObserver(this);
		initComponents();
		initControls();
		setLanguage(ChangeLanguage.currentLanguage);
	}

	private void initComponents() {


		SpringLayout layout = new SpringLayout();
		setLayout(layout);
		setBorder(modeSBorder);
		
		modes = FileHandler.loadModes();
		mList = new JList(modes.toArray());
		mSPane = new JScrollPane(mList);
		
		mList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		mList.setVisibleRowCount(-1);
		

		mListPanel.add(mSPane);
		mListPanel.setBackground(Color.white);
		mListPanel.setBorder(modeMBorder);
		mSPane.setPreferredSize(new Dimension(200,100));
		
		add(title);
		add(mListPanel);
		add(modeFButton);

		layout.putConstraint(SpringLayout.WEST, mListPanel, 40, SpringLayout.WEST, this);
		layout.putConstraint(SpringLayout.NORTH, mListPanel, 50, SpringLayout.NORTH, this);

		layout.putConstraint(SpringLayout.WEST, modeFButton, 250, SpringLayout.WEST, this);
		layout.putConstraint(SpringLayout.NORTH, modeFButton, 170, SpringLayout.NORTH, this);

		setPreferredSize(new Dimension(500,300));
		setVisible(true);
		setBackground(Color.white);
	}

	private void initControls() {
		Action a = (Action) ControlFactory.getObserver(ControlFactory.MODE_CTRL);
		modeFButton.setAction(a);
		
		a.putValue("Mode.modeFButton", modeFButton);
		
		MouseListener ma = (MouseListener) ControlFactory.getObserver(ControlFactory.MODE_CTRL); 
		mList.addMouseListener(ma);
	}
	@Override
	public void setLanguage(ResourceBundle rb) {
//		ResourceBundle rb = ResourceBundle.getBundle(ChangeLanguage.RESOURCE + "Guitext_" + language, Locale.getDefault(), MyClassLoader.getInstance());
		modeFButton.setText(rb.getString("modeFButton"));
		modeMBorder.setTitle(rb.getString("modeMBorder"));
		title.setText(rb.getString("modeTitle"));
		repaint();
	}

}